#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 510;
int map[N][N];
int dis[N], flag[N];
int n, m;
int Dij(int x)
{
	memset(dis, 0x3f, sizeof(dis));
	memset(flag, 0, sizeof(flag));
	dis[x] = 0;
	for (int j = 0; j < n; j++) {
		int pos = -1;
		for (int i = 1; i <= n; i++) {
			if (!flag[i] && (pos == -1 || dis[i] < dis[pos])) {
				pos = i;
			}
		}
		flag[pos] = 1;
		for (int i = 1; i <= n; i++) {
			dis[i] = min(dis[i], dis[pos] + map[pos][i]);	
		}
	}
	if (dis[n] == 0x3f3f3f3f) {
		return -1;
	}
	else {
		return dis[n];
	}
}
int main()
{
	cin >> n >> m;
	memset(map, 0x3f, sizeof(map));
	while (m--) {
		int a, b, c;
		cin >> a >> b >> c;
		map[a][b] = min(map[a][b], c);
	}
	cout << Dij(1) << endl;
	return 0;
}